﻿using RXY_Mall.Application.Finance;
using RXY_Mall.Application.Goods;
using RXY_Mall.Application.Manager;
using RXY_Mall.Application.Order;
using RXY_Mall.Application.Shop;
using RXY_Mall.Application.Statistics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace RXY_Mall.Admin.Controllers
{
    public class HomeController : BaseController
    {
        protected IManagerService _managerService;
        private IFinanceService _financeService;
        private IGoodsService _goodsService;
        private IShopService _shopService;
        private IOrderService _orderService;
        private IStatisticsService _statisticsService;
        public HomeController()
        {
            _managerService = new ManagerService();
            _financeService = new FinanceService();
            _goodsService = new GoodsService();
            _shopService=new ShopService();
            _orderService = new OrderService();
            _statisticsService = new StatisticsService();
        }
        public ActionResult Index()
        {
            var order_statistics = _statisticsService.GetHomeOrderStatistics();
            if (order_statistics == null)
            {
                ViewBag.order_total = 0;
                ViewBag.order_today = 0;

                ViewBag.order_wait_number = 0;
                ViewBag.order_wait_money = 0;

                ViewBag.order_GOD_today = 0;
                ViewBag.order_GOD_today_money =0;
                ViewBag.order_GOG_today = 0;
                ViewBag.order_GOG_today_money = 0;
                ViewBag.order_GMD_today = 0;
                ViewBag.order_GMD_today_money = 0;
                ViewBag.order_GFD_today = 0;
                ViewBag.order_GFD_today_money = 0;

                ViewBag.order_GOD_month = 0;
                ViewBag.order_GOD_month_money = 0;
                ViewBag.order_GOG_month = 0;
                ViewBag.order_GOG_month_money = 0;
                ViewBag.order_GMD_month = 0;
                ViewBag.order_GMD_month_money = 0;
                ViewBag.order_GFD_month = 0;
                ViewBag.order_GFD_month_money = 0;

                ViewBag.order_GOD_year = 0;
                ViewBag.order_GOD_year_money = 0;
                ViewBag.order_GOG_year = 0;
                ViewBag.order_GOG_year_money = 0;
                ViewBag.order_GMD_year = 0;
                ViewBag.order_GMD_year_money = 0;
                ViewBag.order_GFD_year = 0;
                ViewBag.order_GFD_year_money = 0;
            }
            else
            {
                ViewBag.order_total = order_statistics.order_total;
                ViewBag.order_today = order_statistics.order_today;

                ViewBag.order_shipping = order_statistics.order_shipping;
                ViewBag.order_shipping_money = order_statistics.order_shipping_money.ToString("F2");

                ViewBag.order_GOD_today = order_statistics.order_GOD_today;
                ViewBag.order_GOD_today_money = order_statistics.order_GOD_today_money.ToString("F2");
                ViewBag.order_GOG_today = order_statistics.order_GOG_today;
                ViewBag.order_GOG_today_money = order_statistics.order_GOG_today_money.ToString("F2");
                ViewBag.order_GMD_today = order_statistics.order_GMD_today;
                ViewBag.order_GMD_today_money = order_statistics.order_GMD_today_money.ToString("F2");
                ViewBag.order_GFD_today = order_statistics.order_GFD_today;
                ViewBag.order_GFD_today_money = order_statistics.order_GFD_today_money.ToString("F2");

                ViewBag.order_GOD_month = order_statistics.order_GOD_month;
                ViewBag.order_GOD_month_money = order_statistics.order_GOD_month_money.ToString("F2");
                ViewBag.order_GOG_month = order_statistics.order_GOG_month;
                ViewBag.order_GOG_month_money = order_statistics.order_GOG_month_money.ToString("F2");
                ViewBag.order_GMD_month = order_statistics.order_GMD_month;
                ViewBag.order_GMD_month_money = order_statistics.order_GMD_month_money.ToString("F2");
                ViewBag.order_GFD_month = order_statistics.order_GFD_month;
                ViewBag.order_GFD_month_money = order_statistics.order_GFD_month_money.ToString("F2");

                ViewBag.order_GOD_year = order_statistics.order_GOD_year;
                ViewBag.order_GOD_year_money = order_statistics.order_GOD_year_money.ToString("F2");
                ViewBag.order_GOG_year = order_statistics.order_GOG_year;
                ViewBag.order_GOG_year_money = order_statistics.order_GOG_year_money.ToString("F2");
                ViewBag.order_GMD_year = order_statistics.order_GMD_year;
                ViewBag.order_GMD_year_money = order_statistics.order_GMD_year_money.ToString("F2");
                ViewBag.order_GFD_year = order_statistics.order_GFD_year;
                ViewBag.order_GFD_year_money = order_statistics.order_GFD_year_money.ToString("F2");

            }

            var settlement_statistics = _statisticsService.GetSettlementStatistics();
            if (settlement_statistics == null)
            {
                ViewBag.settlement_number = 0;
                ViewBag.settlement_money = 0;
            }
            else
            {
                ViewBag.settlement_number = settlement_statistics.settlement_number;
                ViewBag.settlement_money = settlement_statistics.settlement_money.ToString("F2"); ;
            }

            var user_statistics = _statisticsService.GetUserStatistics();
            if (user_statistics == null)
            {
                ViewBag.user_total = 0;
                ViewBag.user_today = 0;
            }
            else
            {
                ViewBag.user_total = user_statistics.user_total;
                ViewBag.user_today = user_statistics.user_today;
            }

            var vip_statistics = _statisticsService.GetVipStatistics();
            if (vip_statistics == null)
            {
                ViewBag.vip_total = 0;
                ViewBag.vip_today = 0;
            }
            else
            {
                ViewBag.vip_total = vip_statistics.vip_total;
                ViewBag.vip_today = vip_statistics.vip_today;
            }

            var audit_statistics = _statisticsService.GetAuditStatistics(0);
            if (audit_statistics == null)
            {
                ViewBag.user_withdrawal = 0;
                ViewBag.shop_withdrawal = 0;

                ViewBag.shop_check = 0;
                ViewBag.shop_bankcard_check = 0;
            }
            else
            {
                ViewBag.user_withdrawal = audit_statistics.user_withdrawal;
                ViewBag.shop_withdrawal = audit_statistics.shop_withdrawal;

                ViewBag.shop_check = audit_statistics.shop_check;
                ViewBag.shop_bankcard_check = audit_statistics.shop_bankcard_check;
            }

            var goods_statistics = _statisticsService.GetGoodsStatistics();
            if (goods_statistics == null)
            {
                ViewBag.goods_total = 0;
                ViewBag.goods_today = 0;
                ViewBag.goods1_total = 0;
                ViewBag.goods2_total = 0;
                ViewBag.goods3_total = 0;
                ViewBag.goods_publish = 0;
                ViewBag.goods_no_stock = 0;
                ViewBag.goods_no_publish = 0;
            }
            else
            {
                ViewBag.goods_total = goods_statistics.goods_total;
                ViewBag.goods_today = goods_statistics.goods_today;
                ViewBag.goods1_total = goods_statistics.goods1_total;
                ViewBag.goods2_total = goods_statistics.goods2_total;
                ViewBag.goods3_total = goods_statistics.goods3_total;

                ViewBag.goods_publish = goods_statistics.goods_publish;
                ViewBag.goods_no_stock = goods_statistics.goods_no_stock;
                ViewBag.goods_no_publish = goods_statistics.goods_no_publish;
            }
            return View();
        }
        public ActionResult UpdatePwd()
        {
            return View();
        }
        [HttpPost]
        public ActionResult MotifyUserPwd(string oldPassword, string newPassword, string confirmPassword)
        {
            if (newPassword!= confirmPassword)
            {
                return Json(new { status = "n", info = "新密码和确认密码不一致" });
            }
            var result = _managerService.UpdatePassword(MemberId, oldPassword, newPassword);
            if (result.code==1)
            {
                return Json(new { status = "y", info = "修改成功。" });
            }
            else
            {
                return Json(new { status = "n", info = "原密码错误。" });
            }
        }

        [HttpPost]
        public async Task<JsonResult> CheckAllFinance()
        {
            await _financeService.SettlementCheck();
            return Json("success");
        }
        [HttpPost]
        public async Task<JsonResult> DealAllFinance()
        {
            await _financeService.SettlementDeal();
            return Json("success");
        }
        [HttpPost]
        public async Task<JsonResult> DealAllReturn()
        {
            await _financeService.SettlementReturn();
            return Json("success");
        }
        //[HttpPost]
        //public async Task<JsonResult> ResetCategory()
        //{
        //   var result=  await _goodsService.ResetCategory();
        //    return Json(result);
        //}
        //[HttpPost]
        //public  JsonResult SendGroupMsg()
        //{
        //    var result =  _shopService.SendGroupMsg();
        //    return Json(result);
        //}

        public JsonResult GetUserName()
        {
            return Json(UserName, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetRole()
        {
            return Json(MemberId, JsonRequestBehavior.AllowGet);
        }
    }
}